package com.distributedDataBase.interceptor;

import com.alibaba.fastjson.JSON;
import com.distributedDataBase.pojo.Result;
import com.distributedDataBase.pojo.User;
import com.distributedDataBase.util.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.io.PrintWriter;

public class LoginInterceptor implements HandlerInterceptor {

    // 拦截器测试
    /*
    private static int count = 0;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
        count++;
        System.out.println("你不能走 x " + count + ", " + handler);
        // false拦截，true放行
        return false;
    }
    */


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL.toString() + "进入到拦截器...");
        //获取url中的token参数，/cart?token=xxx
        String token = request.getParameter("token");

        //判断本地存储中是否有token
        if(!"null".equals(token)){
            System.out.println("token不为空，token = " + token);
            //解析token
            Claims claims = JWTUtils.checkJWT(token);
            //非法的token或者过期的token,claims是null值
            if(claims!=null){

                User user = new User();

                //从载荷中获取用户的id和用户的username
                Integer id = (Integer) claims.get("id");
                String username = (String) claims.get("username");

                System.out.println("claims也不为空，id = " + id + ", username = " + username);

                user.setId(id);
                user.setUsername(username);

                //将这个user对象放入到request作用域中
                request.setAttribute("user",user);

                //放行
                System.out.println("不拦截，放行");
                return true;
            }
        }else{
            System.out.println("token is null");
            //传统的方式将java对象转换成json对象
            //获取文档输出流
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();

            Result result = new Result("505","sorry,先登录");

            String jsonStr = JSON.toJSONString(result);

            //json字符串返回到客户端
            out.print(jsonStr);
            out.print("hhhhhhhhhhhhhhhh");
            out.flush();

        }
        //解析这个token
        System.out.println(token);
        System.out.println("拦截，不放行");
        return false;//拦截,不放行.
        //return true;//放行
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
